using System;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ScriptDirectives
{
    public class PollIntervalDirective : IScriptDirective
    {
        private readonly Duration pollInterval;

        public PollIntervalDirective([NotNull] string pollInterval)
        {
            if (pollInterval == null) throw new ArgumentNullException("pollInterval");

            var duration = pollInterval.ToDuration();

            if (!PollIntervalCriteria.Accepts(duration))
                throw new InvalidOperationException(string.Format("{0} is not a valid poll interval. {1}", pollInterval, PollIntervalCriteria.GetSummary()));

            this.pollInterval = duration;
        }

        public CompiledNode Apply([NotNull] CompiledScriptModule module)
        {
            if (module == null) throw new ArgumentNullException("module");

            module.PollInterval = pollInterval;
            return new CompiledNode(module);
        }
    }
}